package com.diana.config;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * RocketMq消费者
 */
@Configuration
public abstract class RocketMqConsumerConfig {


    private static final Logger log = LoggerFactory.getLogger(RocketMqConsumerConfig.class);

    public void listener(String topic,String tags) throws MQClientException {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("diana-consumer");
        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.setConsumeMessageBatchMaxSize(10);
        consumer.subscribe(topic,tags);

        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                return RocketMqConsumerConfig.this.dealBody(list);
            }
        });
        consumer.start();
        log.info("rocketMq consumer start--------------topic:"+topic+",tag:"+tags+"-------------");
    }

    public abstract ConsumeConcurrentlyStatus dealBody(List<MessageExt> list);
}
